import 'package:cat_eye/config/string_config.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

/*
* @Package: util
* @ClassName: loading_state_util
* @Author: szj
* @CreateDate: 7/28/21 2:51 PM
*/
//Loading 页面
// ignore: must_be_immutable
class LoadingWidget extends StatelessWidget {
  //正在加载中
  static const LOADING = 0;

  //完成并停止
  static const DONG = 1;

  //错误
  static const ERROR = 2;

  int type = 0;

  //请求错误,点击重新加载时候回调
  Function() reload;

  Widget child;

  LoadingWidget(
      {Key key, this.type = LOADING, @required this.child, this.reload})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    if (type == LOADING) {
      //正在加载中
      return loading();
    } else if (type == DONG) {
      //完成并停止
      // return dong();
      return child;
    } else if (type == ERROR) {
      //错误
      return error(context);
    } else {
      //成功
      return child;
    }
  }

  /*
   *  参考文档:
   *     https://juejin.cn/post/6844903933513564168#heading-22
   */
  //加载中
  Widget loading() {
    return Center(
      child: CupertinoActivityIndicator(
        radius: 30,
        animating: true,
      ),
    );
  }

  //停止
  Widget dong() {
    return Center(
      child: AnimatedOpacity(
        opacity: 0,
        duration: Duration(microseconds: 1000),
        child: CupertinoActivityIndicator(
          radius: 30,
          animating: false,
        ),
      ),
    );
  }

  //请求失败
  Widget error(BuildContext context) {
    return Center(
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Icon(
            Icons.sentiment_dissatisfied_outlined,
            size: 100,
          ),
          Text(StringConfig.HTTP_ERROR),
          InkWell(
            child: Text(StringConfig.RELOAD),
            onTap: () {
              reload();
            },
          )
        ],
      ),
    );
  }
}
